class Solution(object):
    def inorderTraversal(self, root):
        """
        :type root: TreeNode
        :rtype: List[int]
        """
        res = []
        stack = []
        cur = root
        if root is None:
            return res

        stack.append(root)
        visited = set()
        while len(stack) > 0:
            #将栈顶元素的左节点入栈
            top = stack[-1]
            if top.left != None and top not in visited:
                stack.append(top.left)
                visited.add(top)
            else:
                #弹出当前节点
                stack.pop()
                res.append(top.val)
                
                #若右边节点不为空，则入栈
                if top.right != None:
                    stack.append(top.right)
        return res
